
#include "SpaceShooter.h"
#include "defs.h"

void SpaceShooter::loop(){

	std::list<Object*>::iterator	it;
	std::list<Animation>::iterator	animIt;
	std::vector<std::list<Object*>::iterator> elementsToRemove;
	std::vector<std::list<Animation>::iterator> animsToRemove;

	switch(gameState)
	{
		case INGAME:

			spaceBg.loop();

			//Spawn
			if( SDL_GetTicks() >= enemySpawnTimer + 3000 )
			//if(objectList.size() < 1)
			{
				Enemy* enemy;

				if( rand() % 10 <= 4 )
					enemy = new Enemy(enemyShip_1);
				else
				{
					enemy = new Enemy(enemyShip_2);
					enemy->setX(rand() % WND_WIDTH - 40 + 20);
				}
				
				objectList.push_back(enemy);

				enemySpawnTimer = SDL_GetTicks();
			}

			//Objects
			ship.loop();
			for(it = objectList.begin(); it != objectList.end(); it++)
				(*it)->loop();

			//Animations
			for(animIt = animationList.begin(); animIt != animationList.end(); animIt++)
				animIt->loop();

			//Update health
			char str[14];
			sprintf_s(str, "Health: %d", ((Ship*) getPlayerShip())->getShipHealth());
			healthText.setText(str);

			//Update score
			sprintf_s(str, "Score: %4d", score);
			scoreText.setText(str);

			//Remove objects out of screen
			for(it = objectList.begin(); it != objectList.end(); it++)
				if( ((Ship*)*it)->getColRect().x < 0 || ((Ship*)*it)->getColRect().x > WND_WIDTH ||
				    ((Ship*)*it)->getY() < -1000 || ((Ship*)*it)->getY() > WND_HEIGHT )
						elementsToRemove.push_back(it);

			//Remove stopped animations
			for( animIt = animationList.begin(); animIt != animationList.end(); animIt++ )
				if(!animIt->isPlaying())
					animsToRemove.push_back(animIt);

			//Remove destroyed ship and adds explosion animation
			for(it = objectList.begin(); it != objectList.end(); it++)
				if( ((Ship*) *it)->getShipHealth() <= 0 )
				{
					if(((Ship*) *it)->isVisible())
					{
						score++;

						explosionAnim.stop();
						explosionAnim.rewind();

						explosionAnim.setPos((*it)->getX(), (*it)->getY());
						
						explosionAnim.play();
						animationList.push_back(explosionAnim);
					}

					//if no projectiles, it can be erased
					if( ((Ship*) *it)->getWeapon()->getNumberProjectiles() == 0 )
						elementsToRemove.push_back(it);
					else
					{
						((Ship*) *it)->setVisible(false);
						((Ship*) *it)->setStill();
						((Ship*) *it)->getWeapon()->isFiring = false;
					}
				}

			//Remove selected objects
			for(unsigned int i = 0; i < elementsToRemove.size(); i++)
			{
				delete ((Ship*) *elementsToRemove[i]);
				objectList.erase(elementsToRemove[i]);
			}

			for(unsigned int i = 0; i < animsToRemove.size(); i++)
				animationList.erase(animsToRemove[i]);

			elementsToRemove.clear();
			animsToRemove.clear();
						
		break;

		default:
			return;
	}

	// -------------------------------------------------------

	FPS::fpsControl.OnLoop();

#ifdef	_DEBUG
	sprintf_s(windowTitle, "SpaceShooter - FPS: %d", FPS::fpsControl.getFPS());
	SDL_WM_SetCaption(windowTitle, NULL);
#endif

}

